package jjn.round1;

/**
 * @author Jjn
 * @since 2022/8/31 00:16
 */
public class LeetCode733_FloodFill {
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int originalColor = image[sr][sc];
        dfs(image, sr, sc, color, originalColor);
        return image;
    }
    
    private void dfs(int[][] image, int sr, int sc, int color, int originalColor) {
        if (sc < 0 || sc >= image[0].length || sr < 0 || sr >= image.length) {
            return;
        }
        if (image[sr][sc] != originalColor || image[sr][sc] == color) {
            return;
        }
        image[sr][sc] = color;
        dfs(image, sr + 1, sc, color, originalColor);
        dfs(image, sr - 1, sc, color, originalColor);
        dfs(image, sr, sc + 1, color, originalColor);
        dfs(image, sr, sc - 1, color, originalColor);
    }
}
